class LastJiFenTabbar extends egret.DisplayObjectContainer{

    public items:any[] = [];

    private bgObjs:HIRTabbarBitmap[] = [];
    private tfObjs:BaseTextField[] = [];

    public callback:Function;

    public index:number = 0; // 指定位于哪一个tab

    constructor(){
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.viewDidLoad, this);
    }

    private viewDidLoad(){
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.viewDidDisappear, this);
        this.updateViews();
        this.tapIndex(this.index);
    }

    private viewDidDisappear(){
        this.removeEventListener(egret.Event.REMOVED_FROM_STAGE, this.viewDidDisappear, this);
        // 离开视图的时候 要清空
        this.clearViews();
    }

    /*********************************************** private **********************************************************/
    private tapAction(event:egret.TouchEvent):void{
        var index = this.getIndexInArray(event.currentTarget.text, this.items);
        if(index === false) return;
        if(this.index == index) return;
        this.index = index;
        for(var i = 0; i < this.numChildren; i++){
            var child = this.getChildAt(i);
            if(child instanceof HIRTabbarBitmap){
                this.updateBg(child.index === index ? 'tab1' : 'tab2', child);
            }else if(child instanceof BaseTextField){
                if(child.text === event.currentTarget.text){
                    child.textColor = 0xFDE662;
                    child.strokeColor = 0x6E4120;
                }else{
                    child.textColor = 0xEBC275;
                    child.strokeColor = 0x965F41;
                }
            }
        }
        if(this.callback) this.callback(index);
    }

    private tapIndex(index:number){
        if(index >= this.items.length) return;
        for(var i = 0; i < this.numChildren; i++){
            var child = this.getChildAt(i);
            if(child instanceof HIRTabbarBitmap){
                this.updateBg(child.index === index ? 'tab1' : 'tab2', child);
            }else if(child instanceof BaseTextField){
                if(child.text === this.items[index]){
                    child.textColor = 0xFDE662;
                    child.strokeColor = 0x6E4120;
                }else{
                    child.textColor = 0xEBC275;
                    child.strokeColor = 0x965F41;
                }
            }
        }
        if(this.callback) this.callback(index);
    }

    private updateViews(){
        this.clearViews();
        var margin = 10;
        var length = this.items.length;
        if(length < 3) margin = 20;
        for(var i = 0; i < length; i++){
            var bg = this.getBgObj();
            bg.x = (this.width - (bg.width * length + margin * (length - 1))) / 2.0 + bg.anchorOffsetX + i * (bg.width + margin);
            bg.index = i;
            var tf = this.getTfObj();
            tf.text = this.items[i];
            tf.x = bg.x - bg.anchorOffsetX;
            tf.width = bg.width;
            tf.height = bg.height;
            this.addChild(bg);
            this.addChild(tf);
        }
    }

    private getBgObj():HIRTabbarBitmap{
        var bg = this.bgObjs.pop();
        if(!bg){
            bg = new HIRTabbarBitmap();
        }
        this.updateBg('tab2', bg);
        return bg;
    }

    private getTfObj():BaseTextField{
        var tf = this.tfObjs.pop();
        if(!tf){
            tf = new BaseTextField();
            tf.touchEnabled = true;
            tf.size = 25;
            tf.stroke = 2;
            tf.textAlign = egret.HorizontalAlign.CENTER;
            tf.verticalAlign = egret.VerticalAlign.MIDDLE;
            tf.addEventListener(egret.TouchEvent.TOUCH_TAP, this.tapAction, this);
        }
        tf.textColor = 0xFBEBC7;
        tf.strokeColor = 0x965F41;
        return tf;
    }

    private clearViews(){
        if(this.numChildren == 0) return;
        var child = this.getChildAt(0);
        while(child){
            if(child instanceof HIRTabbarBitmap){
                this.bgObjs.push(child);
            }else if(child instanceof BaseTextField){
                this.tfObjs.push(child);
            }
            this.removeChild(child);
            if(this.numChildren == 0) break;
            child = this.getChildAt(0);
        }
    }

    private updateBg(bgName, bg:egret.Bitmap){
        var texture = RES.getRes(bgName);
        bg.texture = texture;
        bg.width = texture.textureWidth;
        bg.height = texture.textureHeight;
        bg.anchorOffsetX = bg.width / 2.0;
    }

    private getIndexInArray(item, arr){
        for(var i = 0; i < arr.length; i++){
            if(arr[i] == item){
                return i;
            }
        }
        return false;
    }
}

class HIRTabbarBitmap extends egret.Bitmap{
    public index = 0;
}